/*
 *         Copyright (c) KUBOTA Corporation and HUDSON SOFT,
 *                  1994, 1995 All Rights Reserved
 *
 *  This software comprises unpublished confidential information of
 *  KUBOTA Corp. and HUDSON SOFT. It may not be used, copied or made 
 *  available to anyone, except in accordance with the license under
 *  which it is furnished.
 */
static char SccsId[] = "@(#)aclook.c	1.9	10/2/95";
#include <farl.h>
float __FarlSqrt( float x );

#include <agl.h>

/*
 * カメラの注視点と上方向ベクトルを指定します。
 */
void
AGLLookAtCamera(fix187 x,fix187 y,fix187 z,fix1015 vx,fix1015 vy,fix1015 vz)
{
     fix1015	sinx, cosx, rdashx;
     float	xdir, ydir, zdir;
     float	rdash, dirnrm;
     float	xndir, yndir, zndir;

     if (__AGLCamera.camChanged >= -1) {
	  xdir = FarlFloat187(x + __AGLCamera.camMatrix187[0][3]);
	  ydir = FarlFloat187(y + __AGLCamera.camMatrix187[1][3]);
	  zdir = FarlFloat187(z + __AGLCamera.camMatrix187[2][3]);
     } else {
	  xdir = FarlFloat187(x);
	  ydir = FarlFloat187(y);
	  zdir = FarlFloat187(z);
     }

     /* 視線方向ベクトルを正規化する */
     dirnrm = __FarlSqrt(xdir*xdir + ydir*ydir + zdir*zdir);

     /* カメラの位置と注視点の距離が 0 の場合は 何もしない */
     if (dirnrm == 0.0)	return;

     xndir = xdir / dirnrm;
     yndir = ydir / dirnrm;
     zndir = zdir / dirnrm;

     /* Ｙ軸回転用のr'を計算する */
     rdash = __FarlSqrt(xndir * xndir + zndir * zndir);
     if (rdash != 0) {
	  /* Ｙ軸回転用のsin, cosを求める */
	  sinx = FarlFix1015(xndir / rdash);
	  cosx = FarlFix1015(zndir / rdash);
	  AGLRotateCameraYSC(~sinx+1, ~cosx+1);
     }

     /* Ｘ軸回転を行う */
     sinx = FarlFix1015(yndir);
     rdashx = FarlFix1015(rdash);
     if (! (sinx == 0 && rdashx == 0x7fff)) {
	  AGLRotateCameraXSC(sinx, rdashx);
     }

     /* View Up Vectorに従ってＺ軸回転を行う */
     if (! (vx == 0 && vy == 0x7fff)) {
	  AGLRotateCameraZSC(~vx+1, vy);
     }
}
